package com.soccertgc.core.game.requests 
{
	import com.soccertgc.core.Position;
	import com.soccertgc.core.game.GameAction;
	import com.soccertgc.core.game.GameActionCard;
	import com.soccertgc.core.game.GameActionRole;
	import com.soccertgc.core.game.GameActionTarget;

	import flash.utils.Dictionary;

	public class GameRequestPlayParams extends GameRequestParams
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// JSON properties names
		protected static const		JSON_PROPERTY_ACTION:String             	= "action";
		protected static const		JSON_PROPERTY_ACTION_TARGET:String          = "actionTarget";
		protected static const		JSON_PROPERTY_PLAYED_CARDS:String			= "cardsPlayed";
		protected static const		JSON_PROPERTY_PLAYED_CARD_ID:String         = "id";
		protected static const		JSON_PROPERTY_PLAYED_CARD_PARAMS:String     = "params";
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _role:GameActionRole;
		private var _action:GameAction;
		private var _target:Position;
		private var _cards:Dictionary; // <GameActionCard, GameActionTarget>
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function GameRequestPlayParams(role:GameActionRole, action:GameAction, target:Position)
		{
			super();
			// Init data
			_role	= role;
			_action	= action;
			_target	= target;
			// Init objects
			_cards = new Dictionary();
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get action():GameAction
		{
			return _action;
		}
		
		public function get role():GameActionRole
		{
			return _role;
		}
		
		public function get target():Position
		{
			return _target;
		}
		
		public function get cards():Dictionary
		{
			return _cards;
		}	
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		protected function createPlayedCardJson(card:GameActionCard, target:GameActionTarget):Object
		{
			// Target json
			var jsonTarget:Object = new Object();
			if (target.hasParams)
			{
				for (var name:String in target.params)
				{
					jsonTarget[name] = target.getParamValue(name);
				}
			}
			// Card JSON
			var jsonCard:Object = new Object();
			jsonCard[JSON_PROPERTY_PLAYED_CARD_ID] = card.instanceId.toString();
			if (target.hasParams) jsonCard[JSON_PROPERTY_PLAYED_CARD_PARAMS] = jsonTarget; 
			// Return JSON card target
			return jsonCard;
		}
		
		override protected function createActionData():Object
		{
			var json:Object = new Object();
			var cards:Array = new Array();
			// Encode all played cards to JSON
			for (var card:GameActionCard in _cards) cards.push(createPlayedCardJson(card, _cards[card]));
			// Encode base data to JSON			
			json[JSON_PROPERTY_ACTION] 			= _action.name;
			json[JSON_PROPERTY_ACTION_TARGET] 	= _target.sign;
			json[JSON_PROPERTY_PLAYED_CARDS] 	= _target.sign;
			// Encoded
			return json;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------

		/**
		 * 
		 * @param card
		 * @param target
		 * 
		 */		
		public function addCard(card:GameActionCard, target:GameActionTarget):void
		{
			// LT_TODO: validare la selezione della carta in qualche modo
			// se vengono fatte bene le selezioni non dovrebbe essere necessario
			// visto che il Gameengine non fornirà la lista delle carte non giocabili a priori
			_cards[card] = target;
		}
		
		/**
		 * 
		 * @param card
		 * 
		 */		
		public function removeCard(card:GameActionCard):void
		{
			delete _cards[card];
		}
		
		/**
		 * 
		 */		
		public function clearCards():void
		{
			for (var card:GameActionCard in _cards)
			{
				delete _cards[card];
			}
		}
		
		
	}
}